package zhihui.mobile.presenter.share;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;
import com.zgm.utils.UiUtils;

import java.io.ByteArrayOutputStream;

import zhihui.mobile.R;
import zhihui.mobile.config.Config;
import zhihui.mobile.listener.ShareListener;

/**
 * Created by 张公铭 on 2017/9/14 0014.
 * 分享工具类
 */

public class SharePresenter
{
    private Context mContext;
    private Tencent mTencent;
    private IWXAPI api;

    public SharePresenter(Context context)
    {
        mContext = context;
        mTencent = Tencent.createInstance(Config.QQ_APP_ID, mContext);
        api = WXAPIFactory.createWXAPI(mContext, Config.WX_APP_ID);
        api.registerApp(Config.WX_APP_ID);
    }

    public void qq(String title, String desc, String imageurl, String url, boolean flag)
    {
        if (mTencent.isSessionValid() && mTencent.getOpenId() == null)
        {
            com.zgm.utils.UiUtils.ToastInCenter(mContext, "您还未安装QQ", 0);
            return;
        }

        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_APP);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, desc);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageurl);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "智惠妈妈");
        if (flag)
        {
            params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        }
        mTencent.shareToQQ((Activity) mContext, params, new ShareListener(mContext));
    }

    public void weixin(String title, String desc, Bitmap bitmap, String url, int flag)
    {
        if (!api.isWXAppInstalled())
        {
            com.zgm.utils.UiUtils.ToastInCenter(mContext, "您还未安装微信", 0);
            return;
        }
        //创建一个WXWebPageObject对象，用于封装要发送的Url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //创建一个WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = desc;
        msg.thumbData = bmpToByteArray(bitmap);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());//transaction字段用于唯一标识一个请求，这个必须有，否则会出错
        req.message = msg;
        //表示发送给朋友圈  WXSceneTimeline  表示发送给朋友  WXSceneSession
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;

        boolean result = api.sendReq(req);
        if (!result)
        {
            UiUtils.ToastInCenter(mContext, "请检查微信是否开启！", 0);
        }
    }

    /**
     * 得到Bitmap的byte
     *
     * @param bmp 图片
     * @return 返回压缩的图片
     */
    private byte[] bmpToByteArray(Bitmap bmp)
    {
        if (bmp == null)
        {
            bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.message_icon);
        }

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        byte[] result = output.toByteArray();
        try
        {
            output.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
}
